package com.google.code.mochaccino.framework.ui.demo.pages;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.freemarker.TemplateManager;
import com.google.code.mochaccino.framework.rest.Root;
import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.ui.demo.SubjectUtil;
import com.google.code.mochaccino.framework.ui.freemarker.TemplateOutput;
import com.google.code.mochaccino.framework.ui.i18n.ResourceBundleUtil;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

/**
 *
 */
@Path( ChangePassword.PATH )
public class ChangePassword {

	private LoginService service;

	private TemplateManager manager;

	public static final String PATH         = "/change_pwd";
	public static final String PATH_INVALID = "/invalid";
	public static final String PATH_SUCCESS = "/success";

	@Inject
	public ChangePassword( TemplateManager manager, LoginService service ) {
		this.manager = manager;
		this.service = service;
	}

	@POST
	@Consumes( MediaType.APPLICATION_FORM_URLENCODED )
	@Produces( MediaType.TEXT_PLAIN )

	public Response changePassword( @FormParam( "username" ) String username, @FormParam( "password" ) String password, @FormParam( "confirmation" ) String confirmation, @FormParam( "token" ) String token ) throws URISyntaxException {

		if ( !password.equals( confirmation ) ) {
			return Response.serverError().entity( PATH + PATH_INVALID ).build();
		}

		if ( !password.matches( "^.*(?=.{8,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*$" ) ) {
			return Response.serverError().entity( PATH + PATH_INVALID ).build();
		}

		if ( !service.validateToken( username, token ) ) {
			return Response.serverError().entity( PATH + PATH_INVALID ).build();
		}

		if ( !service.changePassword( username, token, password, confirmation ) ) {
			return Response.serverError().entity( PATH + PATH_INVALID ).build();
		}

		return Response.ok().entity( PATH + PATH_SUCCESS ).build();
	}

	@Path( PATH_INVALID )
	@GET
	@Produces( MimeTypes.TYPE_CSS )

	public Response changePasswordInvalid_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password_invalid/change_password_invalid.less.ftl" ) ).build();
	}

	@Path( PATH_INVALID )
	@GET
	@Produces( MediaType.TEXT_HTML )

	public Response changePasswordInvalid_html( @QueryParam( "lang" ) String lang ) {
		Locale loc = SubjectUtil.setLocale( lang );
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "env", service.getEnvironment() );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/change_password_invalid/change_password_invalid", loc ) );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password_invalid/change_password_invalid.html.ftl", parameters ) ).build();
	}

	@Path( PATH_SUCCESS )
	@GET
	@Produces( MimeTypes.TYPE_CSS )

	public Response changePasswordSuccess_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password_success/change_password_success.less.ftl" ) ).build();
	}

	@Path( PATH_SUCCESS )
	@GET
	@Produces( MediaType.TEXT_HTML )

	public Response changePasswordSuccess_html( @QueryParam( "lang" ) String lang ) {
		Locale loc = SubjectUtil.setLocale( lang );
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "env", service.getEnvironment() );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/change_password_success/change_password_success", loc ) );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password_success/change_password_success.html.ftl", parameters ) ).build();
	}

	@GET
	@Produces( MimeTypes.TYPE_CSS )

	public Response changePassword_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password/change_password.less.ftl" ) ).build();
	}

	@GET
	@Produces( MediaType.TEXT_HTML )

	public Response changePassword_html( @QueryParam( "lang" ) String lang, @QueryParam( "user" ) String username, @QueryParam( "token" ) String token ) {
		if ( !service.validateToken( username, token ) ) {
			try {
				return Response.temporaryRedirect( new URI( PATH + PATH_INVALID ) ).build();
			} catch ( URISyntaxException e ) {
				throw new WebApplicationException( 500 );
			}
		}
		return changePassword_html( SubjectUtil.setLocale( lang ), username, token, false ).build();
	}

	public ResponseBuilder changePassword_html( Locale loc, String credential, String token, boolean refreshOnSuccess ) {
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "env", service.getEnvironment() );
		parameters.put( "username", credential );
		parameters.put( "token", token );
		parameters.put( "urlChangePwd", PATH );
		parameters.put( "urlChangePwdCancel", Root.PATH_LOGOUT );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/change_password/change_password", loc ) );
		parameters.put( "refreshOnSuccess", refreshOnSuccess );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/change_password/change_password.html.ftl", parameters ) );
	}
}
